package com.example.hrsystem.workflow.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.WorkflowViewModel;
import com.example.hrsystem.workflow.model.WorkflowTask;

import java.util.List;

/**
 * 工作流主界面Activity
 * 展示用户的工作流任务列表和相关操作
 */
public class WorkflowActivity extends AppCompatActivity {
    private WorkflowViewModel workflowViewModel;
    private RecyclerView taskRecyclerView;
    private WorkflowTaskAdapter taskAdapter;
    private TextView emptyView;
    private Button refreshButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workflow);

        // 初始化ViewModel
        workflowViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WorkflowViewModel.class);

        // 初始化UI组件
        initUI();

        // 设置观察者
        setupObservers();

        // 加载数据
        workflowViewModel.loadUserTasks(1); // 示例用户ID为1
    }

    private void initUI() {
        taskRecyclerView = findViewById(R.id.recyclerView_tasks);
        emptyView = findViewById(R.id.text_empty);
        refreshButton = findViewById(R.id.button_refresh);

        // 设置RecyclerView
        taskAdapter = new WorkflowTaskAdapter();
        taskRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        taskRecyclerView.setAdapter(taskAdapter);

        // 设置刷新按钮点击事件
        refreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                workflowViewModel.loadUserTasks(1); // 示例用户ID为1
            }
        });
    }

    private void setupObservers() {
        // 观察任务列表数据
        workflowViewModel.getUserTasks().observe(this, new androidx.lifecycle.Observer<List<WorkflowTask>>() {
            @Override
            public void onChanged(List<WorkflowTask> workflowTasks) {
                if (workflowTasks != null && !workflowTasks.isEmpty()) {
                    taskAdapter.setTasks(workflowTasks);
                    taskRecyclerView.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                } else {
                    taskRecyclerView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }
        });

        // 观察加载状态
        workflowViewModel.getLoading().observe(this, new androidx.lifecycle.Observer<Boolean>() {
            @Override
            public void onChanged(Boolean loading) {
                // 可以在这里显示/隐藏加载指示器
            }
        });

        // 观察错误信息
        workflowViewModel.getError().observe(this, new androidx.lifecycle.Observer<String>() {
            @Override
            public void onChanged(String error) {
                if (error != null && !error.isEmpty()) {
                    // 显示错误信息
                }
            }
        });
        
        // 设置任务列表点击事件
        taskAdapter.setOnTaskClickListener(new WorkflowTaskAdapter.OnTaskClickListener() {
            @Override
            public void onTaskClick(WorkflowTask task) {
                // 跳转到任务处理界面
                Intent intent = new Intent(WorkflowActivity.this, UserTaskHandlerActivity.class);
                intent.putExtra("task", task);
                startActivity(intent);
            }
        });
    }
}